package algorithm.view.maze;

import algorithm.view.common.AlgoHelpler;
import algorithm.view.demo.box.AlgoVisHelper;
import algorithm.view.demo.box.Board;
import algorithm.view.demo.box.GameData;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

public class AlgoFrame extends JFrame{

    private int canvasWidth;
    private int canvasHeight;
    private MazeData mazeData;

    public AlgoFrame(String title, int canvasWidth, int canvasHeight){

        super(title);

        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        AlgoCanvas canvas = new AlgoCanvas();
        setContentPane(canvas);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        setVisible(true);
    }

    public AlgoFrame(String title){

        this(title, 1024, 768);
    }

    public int getCanvasWidth(){return canvasWidth;}
    public int getCanvasHeight(){return canvasHeight;}

    public void render(MazeData mazeData){
        this.mazeData = mazeData;
        repaint();
    }

    private class AlgoCanvas extends JPanel{
        public AlgoCanvas(){
            // 双缓存
            super(true);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D)g;

            // 具体绘制
            int w = canvasWidth/mazeData.getM();
            int h = canvasHeight/mazeData.getL();
            for(int i=0;i<mazeData.getL();i++){
                for(int j=0;j<mazeData.getM();j++){
                    if(mazeData.getData()[i][j]==MazeData.WALL){
                        AlgoHelpler.setColor(g2d,Color.black);
                    }
                    else{
                        AlgoHelpler.setColor(g2d,Color.blue);
                    }

                    if(mazeData.path[i][j]==true){
                        AlgoHelpler.setColor(g2d,Color.yellow);
                    }

                    if(mazeData.result[i][j]==true){
                        AlgoHelpler.setColor(g2d,Color.red);
                    }

                    AlgoHelpler.fillRectangle(g2d,j*w,i*h,w,h);
                }
            }

        }

        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }

    public static void main(String[] args) {
        int a=0,b=2,c=3;
        if(a==0){
            System.out.println("aaaaaaaaaaaaaa");
        }
        if(b==3){
            System.out.println("bbbbbbbbbbbbbbbb");
        }
        else{
            System.out.println("else..................");
        }

    }
}